/**
 * Auto gen via dbpojo-gen.
 */
package com.us.usws.site.ipodium.action;

import javax.inject.Inject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.us.usws.biz.LoginOutWorker;
import com.us.usws.model.domain.system.User;
import com.us.usws.model.flat.system.DOUser;
import com.us.usws.service.ro.UserROService;
import com.us.usws.support.uitls.SiteWebContext;

/**
 * Struts2 action
 * @author robot
 */
public class LoginAction extends ActionSupport {

	private static Log log = LogFactory.getFactory().getInstance(LoginAction.class);

	@Inject
	UserROService service;

	private static final long serialVersionUID = 1L;

	private String username;
	private String password;
	private String imagecode;
	private String act;

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() {
		DOUser u = service.getDOUserByLogin(username, password);

		if (u != null) {
			SiteWebContext wc = new SiteWebContext(ServletActionContext.getRequest(), null);
			LoginOutWorker.ipodiumLogin(u, wc);
			return SUCCESS;
		} else {
			return INPUT;
		}
	}

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#validate()
	 */
	@Override
	public void validate() {
		if (act == null || act.trim().length() == 0) {
			//the 'act' param was not specified, which mean the form's at first show
			//now should not display error messages.
			return;
		}

		if (username == null || username.trim().length() == 0) {
			addActionError(getText("error.empty.username"));
			log.debug("Empty username field.");
			return;
		}
		if (password == null || password.trim().length() == 0) {
			addActionError(getText("error.empty.password"));
			log.debug("Empty password field.");
			return;
		}

		//captcha
		/*
		String captcha = (String) ServletActionContext.getRequest().getSession()
				.getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);
		if (captcha == null || !captcha.equalsIgnoreCase(imagecode)) {
			log.debug("Invalid imagecode=[" + imagecode + "], while captcha=[" + captcha + "].");
			addActionError(getText("error.invalid.captcha"));
			return;
		}
		*/
		User u = service.getUserByLogin(username, password);
		if (u == null) {
			addActionError(getText("login.fail"));
			log.debug("Invalid username/password.");
		}
	}

	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}

	/**
	 * @param username the username to set
	 */
	public void setUsername(String username) {
		this.username = username;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the imagecode
	 */
	public String getImagecode() {
		return imagecode;
	}

	/**
	 * @param imagecode the imagecode to set
	 */
	public void setImagecode(String imagecode) {
		this.imagecode = imagecode;
	}

	/**
	 * @return the act
	 */
	public String getAct() {
		return act;
	}

	/**
	 * @param act the act to set
	 */
	public void setAct(String act) {
		this.act = act;
	}

}